"""
计算昼夜温差
"""
import os.path

from osgeo import gdal
import numpy as np


def computed_lst(day_lst_path, night_lst_path):
    day_lst = gdal.Open(day_lst_path)
    night_lst = gdal.Open(night_lst_path)
    day_lst_arr = day_lst.ReadAsArray()
    night_lst_arr = night_lst.ReadAsArray()
    night_lst_arr[night_lst_arr == 0] = np.nan
    lst_diff_arr = np.abs(day_lst_arr - night_lst_arr)
    # print(lst_diff_arr)
    count = 0
    for i in range(lst_diff_arr.shape[0]):
        for j in range(lst_diff_arr.shape[1]):
            if lst_diff_arr[i][j] < 1:
                print("昼夜温差小于0，白天地表温度：{}，晚上地表温度：{}，昼夜温差：{}".format(day_lst_arr[i][j], night_lst_arr[i][j], lst_diff_arr[i][j]))
                count = count + 1
            if lst_diff_arr[i][j] > 50:
                print("昼夜温差大于50，白天地表温度：{}，晚上地表温度：{}，昼夜温差：{}".format(day_lst_arr[i][j], night_lst_arr[i][j], lst_diff_arr[i][j]))
    print("温差小于1的点数：{}，总数：{}，占比：{:.2f}%".format(count, lst_diff_arr.shape[0] * lst_diff_arr.shape[1], count * 100 / (lst_diff_arr.shape[0] * lst_diff_arr.shape[1])))
    print(np.nanmax(lst_diff_arr))
    print(np.nanmin(lst_diff_arr))


if __name__ == '__main__':
    day_lst = r"G:\test\lst\day_rec_cut"
    night_lst = r"G:\test\lst\night_rec_cut"
    day_lst_list = [os.path.join(day_lst, i) for i in os.listdir(day_lst)]
    night_lst_list = [os.path.join(night_lst, i) for i in os.listdir(night_lst)]
    computed_lst(day_lst_list[0], night_lst_list[0])
